package com.youlu.campus.admin.data.controller;

import com.youlu.campus.admin.utils.AuthUtils;
import com.youlu.campus.common.base.RestResult;
import com.youlu.campus.common.base.RestResultBuilder;
import com.youlu.campus.entity.VO.req.DataSimplingTaskQueryVO;
import com.youlu.campus.entity.data.DataSimplingTask;
import com.youlu.campus.service.data.DataSimplingTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.Objects;

@RestController
public class DataSimplingTaskController {
    @Autowired
    private DataSimplingTaskService dataSimplingTaskService;

    @PostMapping("/data-simpling/task/create")
    public RestResult create(@RequestBody @Valid DataSimplingTask req) {
        String comId = AuthUtils.getComId();
        req.setOptUser(AuthUtils.getLoginName());
        req.setTenantId(comId);
        DataSimplingTask re = dataSimplingTaskService.create(req);
        if (Objects.nonNull(re)) {
            return RestResultBuilder.builder().success().build();
        }
        return RestResultBuilder.builder().failure().build();
    }

    @PostMapping("/data-simpling/task/update")
    public RestResult update(@RequestBody @Valid DataSimplingTask req) {
        boolean re = dataSimplingTaskService.update(req);
        if (re) {
            return RestResultBuilder.builder().success().build();
        }
        return RestResultBuilder.builder().failure().build();
    }

    @PostMapping("/data-simpling/task/list")
    public RestResult list(@RequestBody @Valid DataSimplingTaskQueryVO req) {
        return RestResultBuilder.builder().success(dataSimplingTaskService.list(req)).build();
    }

    @GetMapping("/data-simpling/task/find-one")
    public RestResult findOne(@RequestParam("id") @NotNull String id) {
        return RestResultBuilder.builder().success(dataSimplingTaskService.findOne(id)).build();
    }

    @GetMapping("/data-simpling/task/start")
    public RestResult start(@RequestParam("id") @NotNull String id) {
        return RestResultBuilder.builder().success(dataSimplingTaskService.start(id)).build();
    }
}
